<?php

/*
 * @copyright :     Ienter asia Technologies
 * @author :        Quyet
 * @version         v.1.0
 * @created         25/6/2014
 * @Description :   Model Holiday 
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mmod_holiday extends MYAD_Model {

    protected $table_name = 'm_holiday';

    function __construct() {
        parent::__construct();
    }

    /*
     * @author :        Quyet
     * @created:        25/6/2014
     * @Description:    Get all data for holiday
     * @Input :         (Integer) $page
     * @Output :        (Array) data for view data
     */

    function getAllData($page = 0) {
        /* initialiazion condition */
        $condition = array(
            'where' => array('CompanyCode' => $this->session->userdata('CompanyCode')),
        );
        /* get parameter year search */
        $txtYear = $this->input->post('cmbYear');
        if (!empty($txtYear)) {
            $condition['where']['YEAR(Date)'] = (integer) $txtYear;
        }

        /* load all label */
        $labelData = $this->getLabelData();

        /* load all employee data range */
        $allData = $this->getHoliday($condition);

        /* load pagination */
        $pagiData = array(
            'base_url' => base_url('holiday'),
            'total_rows' => count($allData['data']),
        );
        $pagination = $this->config->item('pagination');
        $offset = isset($pagiData['per_page']) ? $pagiData['per_page'] : $pagination['per_page'];
        $pagi = $this->getPagination($pagiData);

        /* load holiday data range */
        $condition['limit'] = array($page, $offset);
        $condition['order_by'] = 'Date DESC';
        $holidayData = $this->getHoliday($condition);

        /* load config */
        $cmbYear = $this->getYear();
        $data = array(
            'show' => 'index',
            'pagi' => $pagi,
            'message' => $this->lang->line('emp_sli_table_empty_data'),
            'order' => $page,
            'cmbYear' => $cmbYear,
            'year' => $txtYear,
        );
        return array_merge($data, $labelData, $holidayData);
    }

    /*
     * @author :        Quyet
     * @created:        25/6/2014
     * @Description:    Get label data of holiday
     * @Input :         void
     * @Output :        (Array) data label
     */

    function getLabelData() {

        $title = $this->lang->line('holi_title');
        $lblYear = $this->lang->line('holi_lblYear');
        $lblSelect = $this->lang->line('holi_lblSelect');
        $lblNewRegister = $this->lang->line('holi_lblNewRegister');
        $lblOrder = $this->lang->line('holi_table_col_order');
        $lblHolidayDate = $this->lang->line('holi_table_col_holidayDate');
        $lblHolidayDescription = $this->lang->line('holi_table_col_holidayDescription');
        $lblHolidayAction = $this->lang->line('holi_table_col_holidayAction');
        $lblHolidayActive = $this->lang->line('po_table_col_Active');

        $data = array(
            'title' => (empty($title) ? 'List of holidays' : $title),
            'lblYear' => (empty($lblYear) ? 'Year' : $lblYear),
            'lblSelect' => (empty($lblSelect) ? 'Select' : $lblSelect),
            'lblNewRegister' => (empty($lblNewRegister) ? 'New register' : $lblNewRegister),
            'lblOrder' => (empty($lblOrder) ? 'No.' : $lblOrder),
            'lblHolidayDate' => (empty($lblHolidayDate) ? 'Holiday date' : $lblHolidayDate),
            'lblHolidayDescription' => (empty($lblHolidayDescription) ? 'Description' : $lblHolidayDescription),
            'lblHolidayAction' => (empty($lblHolidayAction) ? 'Action' : $lblHolidayAction),
            'lblHolidayActive' => (empty($lblHolidayActive) ? 'Deleted' : $lblHolidayActive),
        );
        return $data;
    }

    /*
     * @author :        Quyet
     * @created:        25/6/2014
     * @Description:    Get salary info by employeeID
     * @Input :         
     *                  (Integer) $employeeID
     *                  (Integer) $limit
     *                  (Integer) $offset
     * @Output :        Array data /FALSE
     */

    function getHoliday($condition = array()) {
        /* get holiday */
        $temp = $this->selectDB($this->table_name, $condition);
        return array('data' => $temp);
    }

    /*
     * @author :        Quyet
     * @created:        25/6/2014
     * @Description:    check validate form add/edit holiday
     * @Input :         void
     * @Output :        boolean
     */

    function validate() {
        /* convert */
        $_POST['txtDate'] = mb_convert_kana($_POST['txtDate'], 'a');

        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('txtDate', $this->lang->line('holi_update_lblDate'), 'required|max_length[10]|callback_validate_date');
        $this->form_validation->set_rules('txtDescription', $this->lang->line('holi_update_lblDescription'), 'max_length[1000]');
        $this->form_validation->set_rules('txtDelete', $this->lang->line('emp_add_btnsubmit'), 'integer|max_length[1]');
        return $this->form_validation->run();
    }

    /*
     * @author :        Quyet
     * @created:        27/6/2014
     * @Description:    update holiday
     * @Input :         (Integer) $id
     * @Output :        boolean
     */

    function updateHoliday($holidayID = 0) {
        /* check duplicate */
        $conditionDuplicate = array(
            'HolidayID !=' => $holidayID,
            'CompanyCode' => $this->session->userdata('CompanyCode'),
            'Date' => $this->input->post('txtDate'),
            'Del_Flag' => FALSE,
        );
        $duplicate = $this->countResult($this->table_name, $conditionDuplicate);
        if ($duplicate) {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('holi_update_lblDate') . ': ' . $this->lang->line('emp_updateDB_duplicate')
            );
        }

        $data = array(
            'Date' => $this->input->post('txtDate'),
            'CompanyCode' => $this->session->userdata('CompanyCode'),
            'Description' => $this->input->post('txtDescription'),
            'Del_Flag' => $this->input->post('txtDelete'),
        );
        $condition = array();
        if ($holidayID) {
            $condition['HolidayID'] = $holidayID;
        }
        $temp = $this->updateDB($this->table_name, $data, $condition);
        if ($temp) {
            return array(
                'flag' => TRUE,
                'message' => $this->lang->line('emp_updateDB_success')
            );
        } else {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('emp_updateDB_fall')
            );
        }
    }

    /*
     * @author :        Quyet
     * @created:        25/6/2014
     * @Description:    basic data for new add holiday
     * @Input :         (Interger) $id
     * @Output :        Array
     */

    function getAddHoliday($holidayID = 0) {
        /* get language info */
        $title = ($holidayID) ? $this->lang->line('holi_update_title') : $this->lang->line('holi_add_title');
        $dialogTitle = $this->lang->line('emp_dialog_title');
        $lblBack = $this->lang->line('holi_update_lblBack');
        $lblDate = $this->lang->line('holi_update_lblDate');
        $lblDescription = $this->lang->line('holi_update_lblDescription');
        $lblSubmit = $this->lang->line('holi_update_lblsubmit');
        $lblDelete = $this->lang->line('po_update_lblDelete');

        /* get holiday */
        $condition = array(
            'where' => array('HolidayID' => $holidayID),
            'first' => TRUE,
        );
        $holiday = $this->selectDB($this->table_name, $condition);
        if (!isset($_POST['btnSubmit']) && $holiday) {
            $emp = array(
                'txtDate' => $this->setDate($holiday->Date),
                'txtDescription' => dQuotes($holiday->Description),
                'txtDelete' => $holiday->Del_Flag,
            );
        } else {
            $emp = array(
                'txtDate' => set_value('txtDate'),
                'txtDescription' => set_value('txtDescription'),
                'txtDelete' => set_value('txtDelete'),
            );
        }

        $temp = array(
            'js' => array('jquery-ui.js'),
            'css' => array('jquery-ui.css'),
            'show' => 'add_holiday',
            /* set label */
            'title' => (empty($title) ? 'Employee salary information Registration' : $title),
            'dialogTitle' => (empty($dialogTitle) ? 'Notification' : $dialogTitle),
            'lblBack' => (empty($lblBack) ? 'Back' : $lblBack),
            'lblDate' => (empty($lblDate) ? 'Date' : $lblDate),
            'lblDescription' => (empty($lblDescription) ? 'Description' : $lblDescription),
            'lblDelete' => (empty($lblDelete) ? 'Deleted' : $lblDelete),
            'submit' => (empty($lblSubmit) ? 'Submit' : $lblSubmit),
            'minYear' => $this->session->userdata('minYear'),
            'maxYear' => $this->session->userdata('maxYear'),
            /* set data */
            'holidayID' => $holidayID,
        );
        return array_merge($temp, $emp);
    }

    /*
     * @author :        Quyet
     * @created:        25/6/2014
     * @Description:    basic data for new add holiday
     * @Input :         (Interger) $id
     * @Output :        Array
     * 
     */

    function countHolidayByID($holidayID = 0) {
        $condition = array(
            'HolidayID' => $holidayID,
            'CompanyCode' => $this->session->userdata('CompanyCode')
        );
        return $this->countResult($this->table_name, $condition);
    }

    /*
     * @author :        Quyet
     * @created:        25/6/2014
     * @Description:    get Year data
     * @Input :         void
     * @Output :        Array
     * 
     */

    function getYear() {
        /* get holiday */
        $condition = array(
            'select' => 'YEAR(DATE) AS  year',
            'where' => array('CompanyCode' => $this->session->userdata('CompanyCode')),
            'group_by' => array('year'),
        );
        return $this->selectDB($this->table_name, $condition);
    }

}

?>
